 package handling.channel.handler;



import client.*;
import com.alibaba.druid.pool.DruidPooledConnection;
import database.DatabaseConnection;
import handling.channel.ChannelServer;
import handling.opcode.BuddyOpcode;
import handling.world.WorldBuddyService;
import handling.world.WorldFindService;
import tools.MaplePacketCreator;
import tools.data.input.LittleEndianAccessor;
import tools.packet.BuddyListPacket;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;




















 public class BuddyListHandler
 {
   private static CharacterIdNameBuddyCapacity getCharacterIdAndNameFromDatabase(String name, String group) throws SQLException {
/*  26 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/*  27 */       PreparedStatement ps = con.prepareStatement("SELECT * FROM characters WHERE name LIKE ?");
/*  28 */       ps.setString(1, name);
/*  29 */       ResultSet rs = ps.executeQuery();
/*  30 */       CharacterIdNameBuddyCapacity ret = null;
/*  31 */       if (rs.next() && 
/*  32 */         rs.getInt("gm") < 3) {
/*  33 */         ret = new CharacterIdNameBuddyCapacity(rs.getInt("id"), rs.getString("name"), group, rs.getInt("buddyCapacity"), true);
       }

/*  36 */       rs.close();
/*  37 */       ps.close();
/*  38 */       return ret;
     }  } public static void BuddyOperation(LittleEndianAccessor slea, MapleClient c) { String addName; int otherCid; String groupName; BuddylistEntry buddylistEntry1, blz, ble;
     boolean linkaccount;
     MapleCharacter addChar;
     BuddylistEntry buddylistEntry2;
/*  43 */     short mode = (short)slea.readByte();
/*  44 */     BuddyOpcode opcode = BuddyOpcode.Companion.getByAction(mode);
/*  45 */     if (opcode == null) {
/*  46 */       System.err.println("未处理好友操作码：" + mode);
       return;
     }
/*  49 */     BuddyList buddylist = c.getPlayer().getBuddylist();
/*  50 */     switch (opcode) {
       case FriendReq_SetFriend:
/*  52 */         addName = slea.readMapleAsciiString();
/*  53 */         groupName = slea.readMapleAsciiString();

/*  55 */         slea.skip(slea.readShort());
/*  56 */         linkaccount = (slea.readByte() == 1);
/*  57 */         if (linkaccount) {
/*  58 */           c.getPlayer().dropMessage(1, "暂时无法添加账号综合好友，\r\n请添加普通好友。");
/*  59 */           c.announce(MaplePacketCreator.enableActions());


           return;
         }

/*  65 */         buddylistEntry2 = buddylist.get(addName);
/*  66 */         if ((addName.getBytes()).length > 13 || (groupName.getBytes()).length > 18) {
           return;
         }
/*  69 */         if (addName.endsWith(c.getPlayer().getName())) {
/*  70 */           c.announce(BuddyOpcode.FriendRes_SetFriend_CantSelf.getPacket()); break;
/*  71 */         }  if (buddylistEntry2 != null) {
/*  72 */           c.announce(BuddyOpcode.FriendRes_SetFriend_AlreadySet.getPacket()); break;
/*  73 */         }  if (buddylist.isFull()) {
/*  74 */           c.announce(BuddyOpcode.FriendRes_SetFriend_FullMe.getPacket());
           break;
         }
         try {
/*  78 */           CharacterIdNameBuddyCapacity charWithId = null;
/*  79 */           int channel = WorldFindService.getInstance().findChannel(addName);

/*  81 */           if (channel > 0) {
/*  82 */             MapleCharacter otherChar = ChannelServer.getInstance(channel).getPlayerStorage().getCharacterByName(addName);
/*  83 */             if (otherChar == null) {
/*  84 */               charWithId = getCharacterIdAndNameFromDatabase(addName, groupName);
/*  85 */             } else if (!otherChar.isIntern() || c.getPlayer().isIntern()) {
/*  86 */               charWithId = new CharacterIdNameBuddyCapacity(otherChar.getId(), otherChar.getName(), groupName, otherChar.getBuddylist().getCapacity(), true);
             }
           } else {
/*  89 */             charWithId = getCharacterIdAndNameFromDatabase(addName, groupName);
           }
/*  91 */           if (charWithId != null) {
/*  92 */             BuddyList.BuddyAddResult buddyAddResult = null;
/*  93 */             if (channel > 0) {
/*  94 */               buddyAddResult = WorldBuddyService.getInstance().requestBuddyAdd(addName, c.getChannel(), c.getPlayer().getId(), c.getPlayer().getName(), c.getPlayer().getLevel(), c.getPlayer().getJob());
             } else {
/*  96 */               try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/*  97 */                 PreparedStatement ps = con.prepareStatement("SELECT COUNT(*) AS buddyCount FROM buddies WHERE characterid = ? AND pending = 0");
/*  98 */                 ps.setInt(1, charWithId.getId());
/*  99 */                 ResultSet rs = ps.executeQuery();
/* 100 */                 if (!rs.next()) {
/* 101 */                   ps.close();
/* 102 */                   rs.close();
/* 103 */                   throw new RuntimeException("Result set expected");
                 }
/* 105 */                 int count = rs.getInt("buddyCount");
/* 106 */                 if (count >= charWithId.getBuddyCapacity()) {
/* 107 */                   buddyAddResult = BuddyList.BuddyAddResult.好友列表已满;
                 }

/* 110 */                 rs.close();
/* 111 */                 ps.close();

/* 113 */                 ps = con.prepareStatement("SELECT pending FROM buddies WHERE characterid = ? AND buddyid = ?");
/* 114 */                 ps.setInt(1, charWithId.getId());
/* 115 */                 ps.setInt(2, c.getPlayer().getId());
/* 116 */                 rs = ps.executeQuery();
/* 117 */                 if (rs.next()) {
/* 118 */                   buddyAddResult = BuddyList.BuddyAddResult.已经是好友关系;
                 }
/* 120 */                 rs.close();
/* 121 */                 ps.close();
               }
             }
/* 124 */             if (buddyAddResult == BuddyList.BuddyAddResult.好友列表已满) {
/* 125 */               c.announce(BuddyOpcode.FriendRes_SetFriend_FullOther.getPacket()); break;
             }
/* 127 */             int displayChannel = -1;
/* 128 */             int i = charWithId.getId();
/* 129 */             if (buddyAddResult == BuddyList.BuddyAddResult.已经是好友关系 && channel > 0) {
/* 130 */               displayChannel = channel;
/* 131 */               notifyRemoteChannel(c, channel, i, groupName, BuddyList.BuddyOperation.添加好友);
/* 132 */             } else if (buddyAddResult != BuddyList.BuddyAddResult.已经是好友关系) {
/* 133 */               try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/* 134 */                 PreparedStatement ps = con.prepareStatement("INSERT INTO buddies (`characterid`, `buddyid`, `groupname`, `pending`) VALUES (?, ?, ?, 1)");
/* 135 */                 ps.setInt(1, charWithId.getId());
/* 136 */                 ps.setInt(2, c.getPlayer().getId());
/* 137 */                 ps.setString(3, groupName);
/* 138 */                 ps.executeUpdate();
/* 139 */                 ps.close();
               }
             }
/* 142 */             buddylist.put(new BuddylistEntry(charWithId.getName(), i, groupName, displayChannel, true));
/* 143 */             c.announce(BuddyListPacket.updateBuddylist(buddylist.getBuddies(), BuddyOpcode.FriendRes_SendSingleFriendInfo.getValue()));
/* 144 */             c.announce(BuddyListPacket.BuddyMess(BuddyOpcode.FriendRes_SetFriend_Done.getValue(), charWithId.getName()));
             break;
           }
/* 147 */           c.announce(BuddyOpcode.FriendRes_SetFriend_UnknownUser.getPacket());
         }
/* 149 */         catch (SQLException e) {
/* 150 */           System.err.println("SQL THROW" + e);
         }
         break;


       case FriendReq_AcceptFriend:
/* 156 */         otherCid = slea.readInt();
/* 157 */         buddylistEntry1 = buddylist.get(otherCid);
/* 158 */         if (!buddylist.isFull() && buddylistEntry1 != null && !buddylistEntry1.isVisible()) {
/* 159 */           int channel = WorldFindService.getInstance().findChannel(otherCid);
/* 160 */           buddylist.put(new BuddylistEntry(buddylistEntry1.getName(), otherCid, "未指定群组", channel, true));
/* 161 */           c.announce(BuddyListPacket.updateBuddylist(buddylist.getBuddies(), BuddyOpcode.FriendRes_LoadAccountIDOfCharacterFriend_Done.getValue()));
/* 162 */           notifyRemoteChannel(c, channel, otherCid, "未指定群组", BuddyList.BuddyOperation.添加好友); break;
         }
/* 164 */         c.announce(BuddyOpcode.FriendRes_SetFriend_FullMe.getPacket());
         break;




       case FriendReq_DeleteFriend:
/* 171 */         otherCid = slea.readInt();
/* 172 */         blz = buddylist.get(otherCid);
/* 173 */         if (blz != null && blz.isVisible()) {
/* 174 */           notifyRemoteChannel(c, WorldFindService.getInstance().findChannel(otherCid), otherCid, blz.getGroup(), BuddyList.BuddyOperation.删除好友);
         }
/* 176 */         buddylist.remove(otherCid);
/* 177 */         c.announce(BuddyListPacket.updateBuddylist(buddylist.getBuddies(), BuddyOpcode.FriendRes_LoadAccountIDOfCharacterFriend_Done.getValue()));
         break;












       case FriendReq_RefuseFriend:
       case FriendReq_RefuseAccountFriend:
/* 193 */         otherCid = slea.readInt();

/* 195 */         ble = buddylist.get(otherCid);
/* 196 */         if (ble == null) {
/* 197 */           c.announce(BuddyOpcode.FriendRes_SetFriend_Unknown.getPacket());
           return;
         }
/* 200 */         c.announce(BuddyListPacket.NoBuddy(otherCid, BuddyOpcode.FriendRes_DeleteFriend_Done.getValue(), (opcode == BuddyOpcode.FriendReq_RefuseAccountFriend)));
/* 201 */         addChar = ChannelServer.getInstance(ble.getChannel()).getPlayerStorage().getCharacterById(ble.getCharacterId());
/* 202 */         addChar.getClient().announce(BuddyListPacket.BuddyMess(BuddyOpcode.FriendRes_DeleteFriend_Unknown.getValue(), c.getPlayer().getName()));
/* 203 */         buddylist.remove(otherCid);
         break;

       case FriendReq_ConvertAccountFriend:
/* 207 */         c.getPlayer().dropMessage(1, "暂时无法添加账号综合好友，\r\n请添加普通好友。");
/* 208 */         c.announce(MaplePacketCreator.enableActions());
         break;
       case FriendReq_ModifyFriend:
/* 211 */         slea.readByte();
/* 212 */         otherCid = slea.readInt();



/* 216 */         slea.skip(4);
/* 217 */         slea.skip(slea.readShort());
/* 218 */         slea.skip(slea.readShort());
/* 219 */         ble = buddylist.get(otherCid);
/* 220 */         if (ble != null) {
/* 221 */           c.announce(BuddyListPacket.updateBuddyNamer(ble, BuddyOpcode.FriendRes_NotifyChange_FriendInfo.getValue()));
         }
         break;
     }  }



   private static void notifyRemoteChannel(MapleClient c, int remoteChannel, int otherCid, String group, BuddyList.BuddyOperation operation) {
/* 229 */     MapleCharacter player = c.getPlayer();
/* 230 */     if (remoteChannel > 0)
/* 231 */       WorldBuddyService.getInstance().buddyChanged(otherCid, player.getId(), player.getName(), c.getChannel(), operation, group); 
   }

   private static final class CharacterIdNameBuddyCapacity
     extends CharacterNameAndId
   {
     private final int buddyCapacity;

     public CharacterIdNameBuddyCapacity(int id, String name, String group, int buddyCapacity, boolean visible) {
/* 240 */       super(id, name, group, visible);
/* 241 */       this.buddyCapacity = buddyCapacity;
     }

     public int getBuddyCapacity() {
/* 245 */       return this.buddyCapacity;
     }
   }
 }


/* Location:              C:\PGX\Server.jar!\handling\channel\handler\BuddyListHandler.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */